package com.talkyun.utils.helper;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegxHelper {

    public static boolean isInclude(String txt, String regx) {
        Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(txt);
        return matcher.find();
    }

    public static String getInclude(String txt, String regx) {
        Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(txt);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    public static String getHeadOther(String txt, String regx) {
        Pattern pattern = Pattern.compile(regx);
        Matcher matcher = pattern.matcher(txt);
        if (matcher.find()) {
            return txt.substring(0, matcher.start());
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(isInclude("conn[12]", "\\[\\d{1,}\\]"));
        System.out.println(getInclude("conn[12]", "\\[\\d{1,}\\]"));
        System.out.println(getHeadOther("conn[12]", "\\[\\d{1,}\\]"));

        System.out.println(getInclude("[2632]", "\\d{1,}"));
    }
}
